package com.example.webdemo.controller;

import com.example.webdemo.bean.user;
import com.example.webdemo.dao.UserDao;
import com.example.webdemo.dto.LoginRequest;
import com.example.webdemo.dto.LoginResponse;
import com.example.webdemo.service.AuthenticationService;
import com.example.webdemo.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserDao userDao;

    private final AuthenticationService authenticationService;

    @Autowired
    public UserController(UserDao userDao, AuthenticationService authenticationService) {
        this.userDao = userDao;
        this.authenticationService = authenticationService;
    }

    //根据ID返回用户信息
    @RequestMapping("/usermessage")
    public user UserMes(HttpSession session) {
        return userDao.findById((Integer) session.getAttribute("userid"));
    }

    @RequestMapping("/alluser")
    public List<user> alluser() {
        return userDao.findall();
    }

    //密码修改
    @RequestMapping("/changepass")
    public JsonResult<LoginResponse> ChangePass(String OldPass, String NewPass, HttpSession session) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUsername(session.getAttribute("username").toString());
        loginRequest.setPassword(OldPass);
        try {
            //利用登录认证确认旧密码正确
            authenticationService.login(loginRequest);
        }catch (Exception e){
            return JsonResult.error("验证旧密码错误: " + e.getMessage());
        }
        user user = new user();
        user.setUsername(loginRequest.getUsername());
        user.setPassword(NewPass);
        try{
            userDao.changepass(user.getUser_id(),NewPass);
        }catch (Exception e){
            return JsonResult.error("更新过程中出错: " + e.getMessage());
        }
        return JsonResult.success("成功修改密码");
    }

    @RequestMapping("/changeuser")
    //个人信息修改
    public JsonResult<Object> ChangeUser(user user, HttpSession session) {
        user.setUsername(session.getAttribute("username").toString());
        user.setUser_id((Integer) session.getAttribute("userid"));
        try {
            userDao.update(user);
        }catch (Exception e){
            return JsonResult.error("更新过程中出错: " + e.getMessage());
        }
        return JsonResult.success("成功更新个人信息: " + user.getUsername());
    }

    @RequestMapping("/deluser")
    //个人信息删除
    public JsonResult<Object> DeleteUser(HttpSession session) {
        try {
            userDao.delete((Integer) session.getAttribute("userid"));
        }catch (Exception e){
            return JsonResult.error("删除过程中出错: " + e.getMessage());
        }
        return JsonResult.success("删除个人信息成功: " + session.getAttribute("username"));
    }

    @RequestMapping("/register")
    //个人信息注册
    public ResponseEntity<Map<String, Object>> registerUser(@RequestBody user user) {
        if (userDao.findByUsername(user.getUsername()) != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT)
                    .body(Map.of("message", "用户名已被注册"));
        }

        int result = userDao.save(user);
        if (result > 0) {
            return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "注册成功"
            ));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("message", "注册失败"));
        }
    }
}
